package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            // 获取文件输入流
            InputStream in = file.getInputStream();
            // 调用方法进行读取
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 课程分类列表（树形）
     *
     * @return
     */
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        // 1.查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        // 2.查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        // 创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 3.封装一级分类
        oneSubjectList.forEach(subject1 -> {
            String id = subject1.getId();
            String title = subject1.getTitle();
            OneSubject oneSubject = new OneSubject();
            oneSubject.setId(id);
            oneSubject.setTitle(title);
            // BeanUtils.copyProperties(subject1,oneSubject);
            // 上面5步可以用 BeanUtils.copyProperties(subject1,oneSubject)代替（也就是代替get->set的过程）
            twoSubjectList.forEach(subject2 -> {
                if (Objects.equals(id, subject2.getParentId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    twoSubject.setId(subject2.getId());
                    twoSubject.setTitle(subject2.getTitle());
                    oneSubject.getChildren().add(twoSubject);
                }
            });
            finalSubjectList.add(oneSubject);
        });

        return finalSubjectList;
    }
}
